package com.ycy.chuwenuniapp.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

@ToString
@Data
public class ResData<T> {
    @ApiModelProperty("响应代码")
    private final Integer code;

    @ApiModelProperty("响应消息")
    private final String message;

    @ApiModelProperty("响应数据")
    private T data;

    public ResData(ResCodeEnum resCodeEnum) {
        this.code = resCodeEnum.getCode();
        this.message = resCodeEnum.getMessage();
    }

    public ResData(ResCodeEnum resCodeEnum, String message) {
        this.code = resCodeEnum.getCode();
        this.message = message;
    }

    public ResData(ResCodeEnum resCodeEnum, T data) {
        this.code = resCodeEnum.getCode();
        this.message = resCodeEnum.getMessage();
        this.data = data;
    }

    public ResData(ResCodeEnum resCodeEnum, String message, T data) {
        this.code = resCodeEnum.getCode();
        this.message = message;
        this.data = data;
    }

    // public Integer getCode() {
    //     return code;
    // }
    //
    // public String getMessage() {
    //     return message;
    // }
    //
    // public T getData() {
    //     return data;
    // }
    //
    // public void setData(T data) {
    //     this.data = data;
    // }

    @JsonIgnore
    public boolean isSuccess() {
        return this.code.equals(ResCodeEnum.SUCCESS.getCode());
    }

    //成功，无参数，默认message
    public static ResData<Void> success() { return new ResData<>(ResCodeEnum.SUCCESS);}
    //成功，无参数，自定义message
    public static ResData<Void> success(String message) { return new ResData<>(ResCodeEnum.SUCCESS,message);}

    //成功，有参数，默认message
    public static <T> ResData<T> success(T data) { return new ResData<>(ResCodeEnum.SUCCESS, data);}
    //成功，有参数，默认message
    public static <T> ResData<T> success(T data,String message) { return new ResData<>(ResCodeEnum.SUCCESS,message, data);}


    //失败，无参数，默认message
    public static ResData<Void> fail() { return new ResData<>(ResCodeEnum.BAD_REQUEST);}
    //失败，无参数，自定义message
    public static ResData<Void> fail(String message) { return new ResData<>(ResCodeEnum.BAD_REQUEST,message);}

    //失败，有参数，默认message
    public static <T> ResData<T> fail(T data) { return new ResData<>(ResCodeEnum.BAD_REQUEST, data);}
    //失败，有参数，默认message
    public static <T> ResData<T> fail(T data,String message) { return new ResData<>(ResCodeEnum.BAD_REQUEST,message, data);}
}
